####################
##MERGING PROCESS###
#MULTILEVEL DATASET#
####################

#MARIANA CARMO DUARTE

#AUGUST 2024

#CODE TO INCLUDE THE FOLLOWING ESS ROUNDS: 2002, 2006, 2010, 2014, (2016), 2018

#Libraries
library(haven)
library(foreign)
library(scales)
library(dplyr)
library(tidyverse)
library(psych)
library(ltm)

#LOAD INDIVIDUAL-LEVEL DATA
setwd("")
ESS_all <- read.spss("ESS_FINAL_2.sav", use.value.labels = TRUE, to.data.frame = TRUE)

#LOAD PARTY-LEVEL DATA
setwd("")
polpargov <- read.spss("PartySystems_CHES_MARPOR_PARLGOV.sav", use.value.labels = TRUE, to.data.frame = TRUE)

#Create "country-group" variable 
ESS_all$g_cntry <- 0
ESS_all$g_cntry[ESS_all$cntry=="Austria" | ESS_all$cntry=="Belgium" | ESS_all$cntry=="Netherlands" | ESS_all$cntry=="United Kingdom" | ESS_all$cntry=="Ireland" | ESS_all$cntry=="Germany" | ESS_all$cntry=="France" | ESS_all$cntry=="Sweden" | ESS_all$cntry=="Denmark" | ESS_all$cntry=="Finland" | ESS_all$cntry=="Luxembourg"] <- 1
ESS_all$g_cntry[ESS_all$cntry=="Portugal" | ESS_all$cntry=="Spain" | ESS_all$cntry=="Italy"| ESS_all$cntry=="Greece" | ESS_all$cntry=="Malta" | ESS_all$cntry=="Cyprus"] <- 2
ESS_all$g_cntry[ESS_all$cntry=="LV" | ESS_all$cntry=="RO" | ESS_all$cntry=="Estonia"| ESS_all$cntry=="Lithuania" | ESS_all$cntry=="Latvia" | ESS_all$cntry=="Poland" | ESS_all$cntry=="Czechia" | ESS_all$cntry=="Hungary" | ESS_all$cntry=="Slovakia"| ESS_all$cntry=="Romania" | ESS_all$cntry=="Bulgaria" | ESS_all$cntry=="Slovenia" | ESS_all$cntry=="Croatia"] <- 3

#Create "CEE" variable
ESS_all$CEE <- 0
ESS_all$CEE[ESS_all$g_cntry==3] <- 1

#Create "SE" variable
ESS_all$SE <- 0
ESS_all$SE[ESS_all$g_cntry==2] <- 1

#Create "imig_index"
#First, I recode the variables (negative direction)
ESS_all$imig_beco[ESS_all$imbgeco=="Bad for the economy"] <- 10
ESS_all$imig_beco[ESS_all$imbgeco=="1"] <- 9
ESS_all$imig_beco[ESS_all$imbgeco=="2"] <- 8
ESS_all$imig_beco[ESS_all$imbgeco=="3"] <- 7
ESS_all$imig_beco[ESS_all$imbgeco=="4"] <- 6
ESS_all$imig_beco[ESS_all$imbgeco=="5"] <- 5
ESS_all$imig_beco[ESS_all$imbgeco=="6"] <- 4
ESS_all$imig_beco[ESS_all$imbgeco=="7"] <- 3
ESS_all$imig_beco[ESS_all$imbgeco=="8"] <- 2
ESS_all$imig_beco[ESS_all$imbgeco=="9"] <- 1
ESS_all$imig_beco[ESS_all$imbgeco=="Good for the economy"] <- 0

ESS_all$imig_bcult[ESS_all$imueclt=="Cultural life undermined"] <- 10
ESS_all$imig_bcult[ESS_all$imueclt=="1"] <- 9
ESS_all$imig_bcult[ESS_all$imueclt=="2"] <- 8
ESS_all$imig_bcult[ESS_all$imueclt=="3"] <- 7
ESS_all$imig_bcult[ESS_all$imueclt=="4"] <- 6
ESS_all$imig_bcult[ESS_all$imueclt=="5"] <- 5
ESS_all$imig_bcult[ESS_all$imueclt=="6"] <- 4
ESS_all$imig_bcult[ESS_all$imueclt=="7"] <- 3
ESS_all$imig_bcult[ESS_all$imueclt=="8"] <- 2
ESS_all$imig_bcult[ESS_all$imueclt=="9"] <- 1
ESS_all$imig_bcult[ESS_all$imueclt=="Cultural life enriched"] <- 0

ESS_all$imig_blive[ESS_all$imwbcnt=="Worse place to live"] <- 10
ESS_all$imig_blive[ESS_all$imwbcnt=="1"] <- 9
ESS_all$imig_blive[ESS_all$imwbcnt=="2"] <- 8
ESS_all$imig_blive[ESS_all$imwbcnt=="3"] <- 7
ESS_all$imig_blive[ESS_all$imwbcnt=="4"] <- 6
ESS_all$imig_blive[ESS_all$imwbcnt=="5"] <- 5
ESS_all$imig_blive[ESS_all$imwbcnt=="6"] <- 4
ESS_all$imig_blive[ESS_all$imwbcnt=="7"] <- 3
ESS_all$imig_blive[ESS_all$imwbcnt=="8"] <- 2
ESS_all$imig_blive[ESS_all$imwbcnt=="9"] <- 1
ESS_all$imig_blive[ESS_all$imwbcnt=="Better place to live"] <- 0

a <- subset(ESS_all, select =c(imig_beco, imig_bcult, imig_blive))

a <- a %>% 
  mutate(immig_index = rowMeans(.))

ESS_all$immig_index <- a$immig_index

#Create "pol_extremism"
ESS_all$pol_extremism[ESS_all$lrscale==0 | ESS_all$lrscale==10] <- 5
ESS_all$pol_extremism[ESS_all$lrscale==1 | ESS_all$lrscale==9] <- 4
ESS_all$pol_extremism[ESS_all$lrscale==2 | ESS_all$lrscale==8] <- 3
ESS_all$pol_extremism[ESS_all$lrscale==3 | ESS_all$lrscale==7] <- 2
ESS_all$pol_extremism[ESS_all$lrscale==4 | ESS_all$lrscale==6] <- 1
ESS_all$pol_extremism[ESS_all$lrscale==5] <- 0

#Create "trstn_index"
ESS_all$trstprl <- as.numeric(ESS_all$trstprl)
ESS_all$trstlgl <- as.numeric(ESS_all$trstlgl)
ESS_all$trstplc <- as.numeric(ESS_all$trstplc)
ESS_all$trstplt <- as.numeric(ESS_all$trstplt)

b <- subset(ESS_all, select =c(trstprl, trstlgl, trstplc, trstplt))

b <- b %>% 
  mutate(trstn_index = rowMeans(.))

ESS_all$trstn_index <- b$trstn_index

#Gender
ESS_all$male[ESS_all$gndr=="Male"] <- 1
ESS_all$male[ESS_all$gndr=="Female"] <- 0

#Create "conf_living"
ESS_all$conf_living[ESS_all$hincfel=="Living comfortably on present income" | ESS_all$hincfel=="Coping on present income"] <- 1
ESS_all$conf_living[ESS_all$hincfel=="Difficult on present income" | ESS_all$hincfel=="Very difficult on present income"] <- 0

#Rural
ESS_all$rural[ESS_all$domicil=="A big city"] <- 0
ESS_all$rural[ESS_all$domicil=="Suburbs or outskirts of big city"] <- 0
ESS_all$rural[ESS_all$domicil=="Town or small city"] <- 1
ESS_all$rural[ESS_all$domicil=="Country village"] <- 1
ESS_all$rural[ESS_all$domicil=="Farm or home in countryside"] <- 1

#WEIGHTS
ESS_all$w_dp <- ESS_all$dweight*ESS_all$pweight
ESS_all$w_pp <- ESS_all$pweight*ESS_all$pspwght

#ADD ELECTION_ID TO THE INDIVIDUAL-LEVEL DATA
#Create the grouping variable "country_year"
ESS_all$country_year <- paste0(ESS_all$cntry, ESS_all$year)
polpargov$country_year <- paste0(polpargov$country.x, polpargov$year)

#Austria
ESS_all$election_id[ESS_all$country_year=="Austria2002"] <- mean(polpargov$election_id[polpargov$country_year=="aus 2002"])
ESS_all$election_id[ESS_all$country_year=="Austria2006"] <- mean(polpargov$election_id[polpargov$country_year=="aus 2006"])
ESS_all$election_id[ESS_all$country_year=="Austria2010"] <- mean(polpargov$election_id[polpargov$country_year=="aus 2010"])
ESS_all$election_id[ESS_all$country_year=="Austria2014"] <- mean(polpargov$election_id[polpargov$country_year=="aus 2014"])
ESS_all$election_id[ESS_all$country_year=="Austria2018"] <- mean(polpargov$election_id[polpargov$country_year=="aus 2019"])

#Belgium
ESS_all$election_id[ESS_all$country_year=="Belgium2002"] <- mean(polpargov$election_id[polpargov$country_year=="be  2002"])
ESS_all$election_id[ESS_all$country_year=="Belgium2006"] <- mean(polpargov$election_id[polpargov$country_year=="be  2006"])
ESS_all$election_id[ESS_all$country_year=="Belgium2010"] <- mean(polpargov$election_id[polpargov$country_year=="be  2010"])
ESS_all$election_id[ESS_all$country_year=="Belgium2014"] <- mean(polpargov$election_id[polpargov$country_year=="be  2014"])
ESS_all$election_id[ESS_all$country_year=="Belgium2016"] <- mean(polpargov$election_id[polpargov$country_year=="be  2017"])
ESS_all$election_id[ESS_all$country_year=="Belgium2018"] <- mean(polpargov$election_id[polpargov$country_year=="be  2019"])

#Bulgaria
ESS_all$election_id[ESS_all$country_year=="Bulgaria2010"] <- mean(polpargov$election_id[polpargov$country_year=="bul 2010"])
ESS_all$election_id[ESS_all$country_year=="Bulgaria2014"] <- mean(polpargov$election_id[polpargov$country_year=="bul 2014"])
ESS_all$election_id[ESS_all$country_year=="Bulgaria2018"] <- mean(polpargov$election_id[polpargov$country_year=="bul 2019"])

#Croatia
ESS_all$election_id[ESS_all$country_year=="Croatia2014"] <- mean(polpargov$election_id[polpargov$country_year=="cro 2014"])
ESS_all$election_id[ESS_all$country_year=="Croatia2018"] <- mean(polpargov$election_id[polpargov$country_year=="cro 2019"])

#Cyrpus
ESS_all$election_id[ESS_all$country_year=="Cyprus2010"] <- mean(polpargov$election_id[polpargov$country_year=="cyp 2010"])
ESS_all$election_id[ESS_all$country_year=="Cyprus2014"] <- mean(polpargov$election_id[polpargov$country_year=="cyp 2014"])
ESS_all$election_id[ESS_all$country_year=="Cyprus2018"] <- mean(polpargov$election_id[polpargov$country_year=="cyp 2019"])

#Czech Republic
ESS_all$election_id[ESS_all$country_year=="Czechia2006"] <- mean(polpargov$election_id[polpargov$country_year=="cz  2006"])
ESS_all$election_id[ESS_all$country_year=="Czechia2010"] <- mean(polpargov$election_id[polpargov$country_year=="cz  2010"])
ESS_all$election_id[ESS_all$country_year=="Czechia2014"] <- mean(polpargov$election_id[polpargov$country_year=="cz  2014"])
ESS_all$election_id[ESS_all$country_year=="Czechia2018"] <- mean(polpargov$election_id[polpargov$country_year=="cz  2019"])

#Denmark
ESS_all$election_id[ESS_all$country_year=="Denmark2002"] <- mean(polpargov$election_id[polpargov$country_year=="dk  2002"])
ESS_all$election_id[ESS_all$country_year=="Denmark2006"] <- mean(polpargov$election_id[polpargov$country_year=="dk  2006"])
ESS_all$election_id[ESS_all$country_year=="Denmark2010"] <- mean(polpargov$election_id[polpargov$country_year=="dk  2010"])
ESS_all$election_id[ESS_all$country_year=="Denmark2014"] <- mean(polpargov$election_id[polpargov$country_year=="dk  2014"])
ESS_all$election_id[ESS_all$country_year=="Denmark2018"] <- mean(polpargov$election_id[polpargov$country_year=="dk  2019"])

#Spain
ESS_all$election_id[ESS_all$country_year=="Spain2002"] <- mean(polpargov$election_id[polpargov$country_year=="esp 2002"])
ESS_all$election_id[ESS_all$country_year=="Spain2006"] <- mean(polpargov$election_id[polpargov$country_year=="esp 2006"])
ESS_all$election_id[ESS_all$country_year=="Spain2010"] <- mean(polpargov$election_id[polpargov$country_year=="esp 2010"])
ESS_all$election_id[ESS_all$country_year=="Spain2014"] <- mean(polpargov$election_id[polpargov$country_year=="esp 2014"])
ESS_all$election_id[ESS_all$country_year=="Spain2016"] <- mean(polpargov$election_id[polpargov$country_year=="esp 2017"])
ESS_all$election_id[ESS_all$country_year=="Spain2018"] <- mean(polpargov$election_id[polpargov$country_year=="esp 2019"])

#Estonia
ESS_all$election_id[ESS_all$country_year=="Estonia2006"] <- mean(polpargov$election_id[polpargov$country_year=="est 2006"])
ESS_all$election_id[ESS_all$country_year=="Estonia2010"] <- mean(polpargov$election_id[polpargov$country_year=="est 2010"])
ESS_all$election_id[ESS_all$country_year=="Estonia2014"] <- mean(polpargov$election_id[polpargov$country_year=="est 2014"])
ESS_all$election_id[ESS_all$country_year=="Estonia2016"] <- mean(polpargov$election_id[polpargov$country_year=="est 2017"])
ESS_all$election_id[ESS_all$country_year=="Estonia2018"] <- mean(polpargov$election_id[polpargov$country_year=="est 2019"])

#Finland
ESS_all$election_id[ESS_all$country_year=="Finland2002"] <- mean(polpargov$election_id[polpargov$country_year=="fin 2002"])
ESS_all$election_id[ESS_all$country_year=="Finland2006"] <- mean(polpargov$election_id[polpargov$country_year=="fin 2006"])
ESS_all$election_id[ESS_all$country_year=="Finland2010"] <- mean(polpargov$election_id[polpargov$country_year=="fin 2010"])
ESS_all$election_id[ESS_all$country_year=="Finland2014"] <- mean(polpargov$election_id[polpargov$country_year=="fin 2014"])
ESS_all$election_id[ESS_all$country_year=="Finland2016"] <- mean(polpargov$election_id[polpargov$country_year=="fin 2017"])
ESS_all$election_id[ESS_all$country_year=="Finland2018"] <- mean(polpargov$election_id[polpargov$country_year=="fin 2019"])

#France
ESS_all$election_id[ESS_all$country_year=="France2002"] <- mean(polpargov$election_id[polpargov$country_year=="fr  2002"])
ESS_all$election_id[ESS_all$country_year=="France2006"] <- mean(polpargov$election_id[polpargov$country_year=="fr  2002"])
ESS_all$election_id[ESS_all$country_year=="France2010"] <- mean(polpargov$election_id[polpargov$country_year=="fr  2010"])
ESS_all$election_id[ESS_all$country_year=="France2014"] <- mean(polpargov$election_id[polpargov$country_year=="fr  2014"])
ESS_all$election_id[ESS_all$country_year=="France2016"] <- mean(polpargov$election_id[polpargov$country_year=="fr  2019"])

#Germany
ESS_all$election_id[ESS_all$country_year=="Germany2002"] <- mean(polpargov$election_id[polpargov$country_year=="ge  2002"])
ESS_all$election_id[ESS_all$country_year=="Germany2006"] <- mean(polpargov$election_id[polpargov$country_year=="ge  2006"])
ESS_all$election_id[ESS_all$country_year=="Germany2010"] <- mean(polpargov$election_id[polpargov$country_year=="ge  2010"])
ESS_all$election_id[ESS_all$country_year=="Germany2014"] <- mean(polpargov$election_id[polpargov$country_year=="ge  2014"])
ESS_all$election_id[ESS_all$country_year=="Germany2016"] <- mean(polpargov$election_id[polpargov$country_year=="ge  2017"])
ESS_all$election_id[ESS_all$country_year=="Germany2018"] <- mean(polpargov$election_id[polpargov$country_year=="ge  2019"])

#Grece
ESS_all$election_id[ESS_all$country_year=="Greece2002"] <- mean(polpargov$election_id[polpargov$country_year=="gr  2002"])
ESS_all$election_id[ESS_all$country_year=="Greece2006"] <- mean(polpargov$election_id[polpargov$country_year=="gr  2006"])
ESS_all$election_id[ESS_all$country_year=="Greece2010"] <- mean(polpargov$election_id[polpargov$country_year=="gr  2010"])

#Hungary
ESS_all$election_id[ESS_all$country_year=="Hungary2006"] <- mean(polpargov$election_id[polpargov$country_year=="hun 2006"])
ESS_all$election_id[ESS_all$country_year=="Hungary2010"] <- mean(polpargov$election_id[polpargov$country_year=="hun 2010"])
ESS_all$election_id[ESS_all$country_year=="Hungary2014"] <- mean(polpargov$election_id[polpargov$country_year=="hun 2014"])
ESS_all$election_id[ESS_all$country_year=="Hungary2016"] <- mean(polpargov$election_id[polpargov$country_year=="hun 2017"])
ESS_all$election_id[ESS_all$country_year=="Hungary2018"] <- mean(polpargov$election_id[polpargov$country_year=="hun 2019"])

#Ireland
ESS_all$election_id[ESS_all$country_year=="Ireland2002"] <- mean(polpargov$election_id[polpargov$country_year=="irl 2002"])
ESS_all$election_id[ESS_all$country_year=="Ireland2006"] <- mean(polpargov$election_id[polpargov$country_year=="irl 2002"])
ESS_all$election_id[ESS_all$country_year=="Ireland2010"] <- mean(polpargov$election_id[polpargov$country_year=="irl 2010"])
ESS_all$election_id[ESS_all$country_year=="Ireland2014"] <- mean(polpargov$election_id[polpargov$country_year=="irl 2014"])
ESS_all$election_id[ESS_all$country_year=="Ireland2016"] <- mean(polpargov$election_id[polpargov$country_year=="irl 2017"])
ESS_all$election_id[ESS_all$country_year=="Ireland2018"] <- mean(polpargov$election_id[polpargov$country_year=="irl 2019"])

#Italy
ESS_all$election_id[ESS_all$country_year=="Italy2002"] <- mean(polpargov$election_id[polpargov$country_year=="it  2002"])
ESS_all$election_id[ESS_all$country_year=="Italy2006"] <- mean(polpargov$election_id[polpargov$country_year=="it  2006"])
ESS_all$election_id[ESS_all$country_year=="Italy2010"] <- mean(polpargov$election_id[polpargov$country_year=="it  2010"])
ESS_all$election_id[ESS_all$country_year=="Italy2014"] <- mean(polpargov$election_id[polpargov$country_year=="it  2014"])
ESS_all$election_id[ESS_all$country_year=="Italy2016"] <- mean(polpargov$election_id[polpargov$country_year=="it  2017"])
ESS_all$election_id[ESS_all$country_year=="Italy2018"] <- mean(polpargov$election_id[polpargov$country_year=="it  2019"])

#Latvia
ESS_all$election_id[ESS_all$country_year=="Latvia2006"] <- mean(polpargov$election_id[polpargov$country_year=="lat 2006"])
ESS_all$election_id[ESS_all$country_year=="Latvia2010"] <- mean(polpargov$election_id[polpargov$country_year=="lat 2010"])
ESS_all$election_id[ESS_all$country_year=="Latvia2014"] <- mean(polpargov$election_id[polpargov$country_year=="lat 2014"])
ESS_all$election_id[ESS_all$country_year=="Latvia2016"] <- mean(polpargov$election_id[polpargov$country_year=="lat 2017"])
ESS_all$election_id[ESS_all$country_year=="Latvia2018"] <- mean(polpargov$election_id[polpargov$country_year=="lat 2019"])

#Lithuania
ESS_all$election_id[ESS_all$country_year=="Lithuania2006"] <- mean(polpargov$election_id[polpargov$country_year=="lith2006"])
ESS_all$election_id[ESS_all$country_year=="Lithuania2010"] <- mean(polpargov$election_id[polpargov$country_year=="lith2010"])
ESS_all$election_id[ESS_all$country_year=="Lithuania2014"] <- mean(polpargov$election_id[polpargov$country_year=="lith2014"])
ESS_all$election_id[ESS_all$country_year=="Lithuania2016"] <- mean(polpargov$election_id[polpargov$country_year=="lith2017"])
ESS_all$election_id[ESS_all$country_year=="Lithuania2018"] <- mean(polpargov$election_id[polpargov$country_year=="lith2019"])

#Luxembourg
ESS_all$election_id[ESS_all$country_year=="Luxembourg2010"] <- mean(polpargov$election_id[polpargov$country_year=="lux 2010"])
ESS_all$election_id[ESS_all$country_year=="Luxembourg2014"] <- mean(polpargov$election_id[polpargov$country_year=="lux 2014"])
ESS_all$election_id[ESS_all$country_year=="Luxembourg2016"] <- mean(polpargov$election_id[polpargov$country_year=="lux 2017"])
ESS_all$election_id[ESS_all$country_year=="Luxembourg2018"] <- mean(polpargov$election_id[polpargov$country_year=="lux 2019"])

#Malta
ESS_all$election_id[ESS_all$country_year=="Malta2014"] <- mean(polpargov$election_id[polpargov$country_year=="mal 2014"])
ESS_all$election_id[ESS_all$country_year=="Malta2018"] <- mean(polpargov$election_id[polpargov$country_year=="mal 2019"])

#Netherlands
ESS_all$election_id[ESS_all$country_year=="Netherlands2002"] <- mean(polpargov$election_id[polpargov$country_year=="nl  2002"])
ESS_all$election_id[ESS_all$country_year=="Netherlands2006"] <- mean(polpargov$election_id[polpargov$country_year=="nl  2006"])
ESS_all$election_id[ESS_all$country_year=="Netherlands2010"] <- mean(polpargov$election_id[polpargov$country_year=="nl  2010"])
ESS_all$election_id[ESS_all$country_year=="Netherlands2014"] <- mean(polpargov$election_id[polpargov$country_year=="nl  2014"])
ESS_all$election_id[ESS_all$country_year=="Netherlands2016"] <- mean(polpargov$election_id[polpargov$country_year=="nl  2017"])
ESS_all$election_id[ESS_all$country_year=="Netherlands2018"] <- mean(polpargov$election_id[polpargov$country_year=="nl  2019"])

#Poland
ESS_all$election_id[ESS_all$country_year=="Poland2006"] <- mean(polpargov$election_id[polpargov$country_year=="pol 2006"])
ESS_all$election_id[ESS_all$country_year=="Poland2010"] <- mean(polpargov$election_id[polpargov$country_year=="pol 2010"])
ESS_all$election_id[ESS_all$country_year=="Poland2014"] <- mean(polpargov$election_id[polpargov$country_year=="pol 2014"])
ESS_all$election_id[ESS_all$country_year=="Poland2016"] <- mean(polpargov$election_id[polpargov$country_year=="pol 2017"])
ESS_all$election_id[ESS_all$country_year=="Poland2018"] <- mean(polpargov$election_id[polpargov$country_year=="pol 2019"])

#Portugal
ESS_all$election_id[ESS_all$country_year=="Portugal2002"] <- mean(polpargov$election_id[polpargov$country_year=="por 2002"])
ESS_all$election_id[ESS_all$country_year=="Portugal2006"] <- mean(polpargov$election_id[polpargov$country_year=="por 2006"])
ESS_all$election_id[ESS_all$country_year=="Portugal2010"] <- mean(polpargov$election_id[polpargov$country_year=="por 2010"])
ESS_all$election_id[ESS_all$country_year=="Portugal2014"] <- mean(polpargov$election_id[polpargov$country_year=="por 2014"])
ESS_all$election_id[ESS_all$country_year=="Portugal2016"] <- mean(polpargov$election_id[polpargov$country_year=="por 2017"])
ESS_all$election_id[ESS_all$country_year=="Portugal2018"] <- mean(polpargov$election_id[polpargov$country_year=="por 2019"])

#Romania
ESS_all$election_id[ESS_all$country_year=="RO2006"] <- mean(polpargov$election_id[polpargov$country_year=="rom 2006"])
ESS_all$election_id[ESS_all$country_year=="RO2010"] <- mean(polpargov$election_id[polpargov$country_year=="rom 2010"])
ESS_all$election_id[ESS_all$country_year=="RO2014"] <- mean(polpargov$election_id[polpargov$country_year=="rom 2014"])
ESS_all$election_id[ESS_all$country_year=="RO2016"] <- mean(polpargov$election_id[polpargov$country_year=="rom 2017"])
ESS_all$election_id[ESS_all$country_year=="RO2018"] <- mean(polpargov$election_id[polpargov$country_year=="rom 2019"])

#Slovenia
ESS_all$election_id[ESS_all$country_year=="Slovenia2006"] <- mean(polpargov$election_id[polpargov$country_year=="sle 2006"])
ESS_all$election_id[ESS_all$country_year=="Slovenia2010"] <- mean(polpargov$election_id[polpargov$country_year=="sle 2010"])
ESS_all$election_id[ESS_all$country_year=="Slovenia2014"] <- mean(polpargov$election_id[polpargov$country_year=="sle 2014"])
ESS_all$election_id[ESS_all$country_year=="Slovenia2016"] <- mean(polpargov$election_id[polpargov$country_year=="sle 2017"])
ESS_all$election_id[ESS_all$country_year=="Slovenia2018"] <- mean(polpargov$election_id[polpargov$country_year=="sle 2019"])

#Slovakia
ESS_all$election_id[ESS_all$country_year=="Slovakia2006"] <- mean(polpargov$election_id[polpargov$country_year=="slo 2006"])
ESS_all$election_id[ESS_all$country_year=="Slovakia2010"] <- mean(polpargov$election_id[polpargov$country_year=="slo 2010"])
ESS_all$election_id[ESS_all$country_year=="Slovakia2014"] <- mean(polpargov$election_id[polpargov$country_year=="slo 2014"])
ESS_all$election_id[ESS_all$country_year=="Slovakia2016"] <- mean(polpargov$election_id[polpargov$country_year=="slo 2017"])
ESS_all$election_id[ESS_all$country_year=="Slovakia2018"] <- mean(polpargov$election_id[polpargov$country_year=="slo 2019"])

#Sweden
ESS_all$election_id[ESS_all$country_year=="Sweden2002"] <- mean(polpargov$election_id[polpargov$country_year=="swe 2002"])
ESS_all$election_id[ESS_all$country_year=="Sweden2006"] <- mean(polpargov$election_id[polpargov$country_year=="swe 2006"])
ESS_all$election_id[ESS_all$country_year=="Sweden2010"] <- mean(polpargov$election_id[polpargov$country_year=="swe 2010"])
ESS_all$election_id[ESS_all$country_year=="Sweden2014"] <- mean(polpargov$election_id[polpargov$country_year=="swe 2014"])
ESS_all$election_id[ESS_all$country_year=="Sweden2016"] <- mean(polpargov$election_id[polpargov$country_year=="swe 2017"])
ESS_all$election_id[ESS_all$country_year=="Sweden2018"] <- mean(polpargov$election_id[polpargov$country_year=="swe 2019"])

#United Kingdom
ESS_all$election_id[ESS_all$country_year=="United Kingdom2002"] <- mean(polpargov$election_id[polpargov$country_year=="uk  2002"])
ESS_all$election_id[ESS_all$country_year=="United Kingdom2006"] <- mean(polpargov$election_id[polpargov$country_year=="uk  2006"])
ESS_all$election_id[ESS_all$country_year=="United Kingdom2010"] <- mean(polpargov$election_id[polpargov$country_year=="uk  2010"])
ESS_all$election_id[ESS_all$country_year=="United Kingdom2014"] <- mean(polpargov$election_id[polpargov$country_year=="uk  2014"])
ESS_all$election_id[ESS_all$country_year=="United Kingdom2016"] <- mean(polpargov$election_id[polpargov$country_year=="uk  2017"])
ESS_all$election_id[ESS_all$country_year=="United Kingdom2018"] <- mean(polpargov$election_id[polpargov$country_year=="uk  2019"])

#MERGE THE 2-LEVEL DATA

FINAL <- merge(ESS_all, polpargov, by = "election_id", all = TRUE)
dta <- filter(FINAL, !is.na(election_id))
DATA_FINAL <- dta%>%drop_na(cntry.x)

setwd("")
write_sav(DATA_FINAL, "DATA_FINAL.sav")
